#include "Bsp_cfg.h"

#include "Bsp_adc.h"
#include "Bsp_can.h"
#include "Bsp_dac.h"
#include "debug.h"
#include "DID.h"
#include "eeprom.h"
#include "SVD.h"
#include "tick.h"
#include "uds_port.h"

static void data_encrypted_handle(void);
static void pin_init(void);

void MF_BSTIM16_TimerBase_Init(void)
{
    FL_BSTIM16_InitTypeDef TimerBase_InitStruct;

    TimerBase_InitStruct.prescaler       = 31;
    TimerBase_InitStruct.autoReload      = 499;
    TimerBase_InitStruct.autoReloadState = FL_ENABLE;
    TimerBase_InitStruct.clockSource     = FL_CMU_BSTIM16_CLK_SOURCE_APBCLK;

    FL_BSTIM16_Init(BSTIM16, &TimerBase_InitStruct);
}

/**
 * @brief  BSTIM32 Interrupt Initialization function
 * @param  void
 * @retval None
 */
void MF_BSTIM16_Interrupt_Init(void)
{

    FL_BSTIM16_SetTriggerOutput(BSTIM16, FL_BSTIM16_TRGO_UPDATE); /*配置BSTIM16 Trigger事件*/

    FL_BSTIM16_ClearFlag_Update(BSTIM16);
    FL_BSTIM16_EnableIT_Update(BSTIM16);
}

void MF_NVIC_Init(void)
{
    FL_NVIC_ConfigTypeDef InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, BSTIM_IRQn);
}

void Time_Init(void)
{

    /* Initial BSTIM32 */
    MF_BSTIM16_TimerBase_Init();
    MF_BSTIM16_Interrupt_Init();

    /* Initial NVIC */
    MF_NVIC_Init();
}

void IWDT_Init(void)
{
    FL_IWDT_InitTypeDef IWDT_InitStruct;

    IWDT_InitStruct.iwdtWindows    = 0;
    IWDT_InitStruct.overflowPeriod = FL_IWDT_PERIOD_1000MS;

    FL_IWDT_Init(IWDT, &IWDT_InitStruct);
#if 0
    FL_IWDT_ClearFlag_EnterWindow(IWDT);
    FL_IWDT_EnableIT_EnterWindow(IWDT);
#endif
}

void BSP_init(void)
{
    /* Initialize FL Driver Library */
    FL_Init();

    /* TODO 31号pin PD6暂设置为浮空 */
    pin_init();

    /* 使能SVD,阈值4.16V(falling)~4.284V(rising) */
    SVD_Init(SVD_MONTIOR_VDD, FL_SVD_WARNING_THRESHOLD_GROUP11, FL_SVD_REFERENCE_1P0V);

    /* 确认SVD监测结果是否高于阈值，如否则持续等待 */
    while (false == SVD_Result_Confirmed(SVD_HIGHER_THRESHOLD, 2000U /*us*/));

    /* 使能BOR为2.0V */
    RMU_BOR_Init(FL_RMU_BOR_THRESHOLD_2P00V);

    /* Configure the system clock */
    MF_Clock_Init();

    rt_tick_init();

    Time_Init();
    /* Initialize all configured peripherals */
    Bsp_DAC_init();
    Bsp_ADC_init();

    VME2P_Initialize();
    uds_init();
    BSp_CAN_init();

    IWDT_Init();

    AES_Init(FL_AES_KEY_LENGTH_128B, FL_AES_CIPHER_CTR, FL_AES_OPERATION_MODE_ENCRYPTION,
             FL_AES_DATA_TYPE_32B);

    /* 读取加密数据 */
    data_encrypted_handle();

    // TODO:看门狗暂不开启
}

static void pin_init(void)
{
    // FL_GPIO_InitTypeDef gpio_init_config;

    // gpio_init_config.pin          = FL_GPIO_PIN_6;
    // gpio_init_config.mode         = FL_GPIO_MODE_INPUT;
    // gpio_init_config.outputType   = FL_GPIO_OUTPUT_PUSHPULL;
    // gpio_init_config.pull         = FL_DISABLE;
    // gpio_init_config.remapPin     = FL_DISABLE;
    // gpio_init_config.analogSwitch = FL_DISABLE;

    // FL_GPIO_Init(GPIOD, &gpio_init_config);
}

static void data_encrypted_handle(void)
{
    uint8_t flash_data;
    if (VME2P_Read(VM_INDEX_DATA_ENCRYPT, &flash_data, 1) == 1) {
        is_data_encrypt = (bool)flash_data;
        LOG("data_encrypt = %s\r\n", is_data_encrypt ? "true" : "false");
    }
    else {
        LOG("data_encrypt read fail\r\n");
    }
}
